package guiTesting;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.atlassian.crowd.exception.ApplicationPermissionException;
import com.atlassian.crowd.exception.InvalidAuthenticationException;
import com.atlassian.crowd.exception.OperationFailedException;
import com.atlassian.crowd.model.user.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

public class SecondGUi extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldSearchTerm;
	private JTable tableSearchResults;
	private RetrieveUserInfo userInfo; 
	static Vector columnNamesV ;
	private JButton searchButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SecondGUi frame = new SecondGUi();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SecondGUi() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 616, 429);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel MainPanel = new JPanel();
		MainPanel.setBorder(new CompoundBorder());
		contentPane.add(MainPanel, BorderLayout.CENTER);

		textFieldSearchTerm = new JTextField();
		textFieldSearchTerm.setToolTipText("Enter search term here");
		textFieldSearchTerm.setColumns(10);


		JButton searchCancelButton = new JButton("Cancel");



		searchButton = new JButton("Search");
		
		JPanel panel = new JPanel();

		GroupLayout gl_MainPanel = new GroupLayout(MainPanel);
		gl_MainPanel.setHorizontalGroup(
			gl_MainPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_MainPanel.createSequentialGroup()
					.addGroup(gl_MainPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_MainPanel.createSequentialGroup()
							.addGap(104)
							.addGroup(gl_MainPanel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(textFieldSearchTerm)
								.addGroup(gl_MainPanel.createSequentialGroup()
									.addComponent(searchButton, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(searchCancelButton, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_MainPanel.createSequentialGroup()
							.addContainerGap()
							.addComponent(panel, GroupLayout.PREFERRED_SIZE, 578, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_MainPanel.setVerticalGroup(
			gl_MainPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_MainPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(textFieldSearchTerm, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_MainPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(searchButton, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(searchCancelButton, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 260, GroupLayout.PREFERRED_SIZE))
		);



		//tableSearchResults = new JTable();
		//scrollPane.setColumnHeaderView(tableSearchResults);
		MainPanel.setLayout(gl_MainPanel);
		
		CreateEvents();
	}
	
	private void CreateEvents (){
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				userInfo = new RetrieveUserInfo (); 
				try {
					List<User> searchResults = userInfo.searchUsers(textFieldSearchTerm.getText());
					Vector rowData = new Vector();
					for (int i=0; i<searchResults.size(); i++){
						User uz = searchResults.get(i); 
						Vector colData = new Vector(Arrays.asList( uz.getFirstName(), uz.getLastName(),uz.getEmailAddress(), uz.getDisplayName(),uz.isActive()));
						
						String[] columnNames  = { "First Name ", "Last Name","Email" , "Display Name", "Is Active"};

						rowData.add(colData);
						columnNamesV = new Vector(Arrays.asList(columnNames));
					}
					tableSearchResults = new JTable(rowData, columnNamesV);
					JScrollPane scrollPaneSearchResults = new JScrollPane(tableSearchResults);
					getContentPane().add(scrollPaneSearchResults).layout();
					//scrollPaneSearchResults.add(tableSearchResults);
			       
			        

					//tableSearchResults.setName("search Results");
					//scrollPaneSearchResults.add(tableSearchResults.getTableHeader(), BorderLayout.NORTH);
					//tableSearchResults = new JTable();
					
					
					
				} catch (OperationFailedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvalidAuthenticationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ApplicationPermissionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
	}
}
